// https://leetcode.cn/problems/bao-han-minhan-shu-de-zhan-lcof/description/

// 题干：请你设计一个 最小栈 。它提供 push ，pop ，top 操作，并能在常数时间内检索到最小元素的栈

// 示例：输入：
//          ["MinStack", "push", "push", "push", "getMin", "pop", "top", "getMin"]
//          [[], [-2], [2], [-3], [], [], [], []]
//      输出：[null,null,null,null,-3,null,2,-2]

// 碎语：用两个栈来实现，同时注意push的时候数据和最小数据栈顶数据相同也要压栈

#define  _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
using namespace std;

#include <stack>

class MinStack 
{
public:
   
    void push(int x)
    {
        _Elem.push(x);

        if (_min.empty() || _Elem.top() <= _min.top()) {
            _min.push(x);
        }
    }

    void pop()
    {
        if (_Elem.top() == _min.top()) {
            _min.pop();
        }
        _Elem.pop();
    }

    int top()
    {
        return _Elem.top();
    }

    int getMin()
    {
        return _min.top();
    }

private:
    stack<int> _min;
    stack<int> _Elem;
};

int main()
{
    MinStack* obj = new MinStack();

    obj->push(1);
    obj->push(4);
    obj->push(2);
    obj->push(6);
    obj->push(3);

    obj->pop();

    cout << obj->top() << endl;
    cout << obj->getMin() << endl;

    return 0;
}